//
//  AppDelegate+umeng.m
//  des
//
//  Created by white－bird on 16/10/31.
//  Copyright © 2016年 white－bird. All rights reserved.
//

#import "AppDelegate+umeng.h"
#import <objc/runtime.h>
Boolean isForeground = YES;
@implementation AppDelegate (umeng)
/*
 * 当此文件自行加载时即会触发的方法
 */
+(void)load
{
    NSLog(@"WhiteBirdUP--->AppDelegateUmeng Load");
}
/*
 * 扩展ionic生成的xcode工程的系统方法,可用于获取deviceToken
 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"WhiteBirdUP--->deivceToken:%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""]);
}
/*
 * 接受推送通知之后所触发的方法
 */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    //关闭友盟自带的前台弹出框
    [UMessage setAutoAlert:NO];
    [UMessage didReceiveRemoteNotification:userInfo];

    NSLog(@"WhiteBird--->%@",userInfo);
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:userInfo];
    if(isForeground)
    {
        [dic setObject:@"YES" forKey:@"isForeground"];
    }
    else
    {
        [dic setObject:@"NO" forKey:@"isForeground"];
    }
    NSNotification *def = [NSNotification notificationWithName:@"PUSHKEY" object:dic];
    [[NSNotificationCenter defaultCenter] postNotification:def];

    //如需原生方式自定义接受推送通知的内容,可在此处自行发挥

    //定制自定的的弹出框
    //if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
    //{
    //    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题"
    //                                                        message:@"Test On ApplicationStateActive"
    //                                                       delegate:self
    //                                              cancelButtonTitle:@"确定"
    //                                              otherButtonTitles:nil];
    //
    //    [alertView show];
    //}

    [UMessage didReceiveRemoteNotification:userInfo];



}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    isForeground = YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    isForeground = NO;
}
@end
